﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using MvcWebRole1.Helpers;
using System;
using System.Linq;
using System.Security.Principal;

namespace MvcWebRole1.Tests.Helpers
{
    [TestClass]
    public class BbsHelperTest
    {
        [TestMethod]
        public void GetNickNameFromUserShouldReturnNickNameWhenUserNameContainsAt()
        {
            // 準備
            var user = new GenericPrincipal(new GenericIdentity("1234567@user"), new string[] { });

            // 実行
            var result = BbsHelper.GetNickNameFromUser(user);

            // アサート
            Assert.AreEqual("user", result);
        }

        [TestMethod]
        public void GetNickNameFromUserShouldReturnIdentityNameWhenUserNameDoesNotContainAt()
        {
            // 準備
            var user = new GenericPrincipal(new GenericIdentity("user"), new string[] { });

            // 実行
            var result = BbsHelper.GetNickNameFromUser(user);

            // アサート
            Assert.AreEqual("user", result);
        }

        [TestMethod]
        public void GetZipCodeFromUserShouldReturnZipCodeWhenUserNameContainsAt()
        {
            // 準備
            var user = new GenericPrincipal(new GenericIdentity("1234567@user"), new string[] { });

            // 実行
            var result = BbsHelper.GetZipCodeFromUser(user);

            // アサート
            Assert.AreEqual("1234567", result);
        }

        [TestMethod]
        public void GetZipCodeFromUserShouldReturnEmptyWhenUserNameDoesNotContainAt()
        {
            // 準備
            var user = new GenericPrincipal(new GenericIdentity("user"), new string[] { });

            // 実行
            var result = BbsHelper.GetZipCodeFromUser(user);

            // アサート
            Assert.AreEqual("", result);
        }

        [TestMethod]
        public void SplitTagListShouldReturnEmptyWhenItIsPassedNull()
        {
            Assert.AreEqual(0, BbsHelper.SplitTagList(null).Count());
        }

        [TestMethod]
        public void SplitTagListShouldReturnEmptyWhenItIsPassedEmptyString()
        {
            Assert.AreEqual(0, BbsHelper.SplitTagList("").Count());
        }

        [TestMethod]
        public void SplitTagListShouldReturnEmptyWhenItIsPassedWhiteSpaceOnly()
        {
            Assert.AreEqual(0, BbsHelper.SplitTagList(" \r\n ").Count());
        }

        [TestMethod]
        public void SplitTagListShouldReturnPassedValueWhenItIsPassedSingleLine()
        {
            var result =  BbsHelper.SplitTagList("TAG1").ToList();
            Assert.AreEqual(1, result.Count());
            Assert.AreEqual("TAG1", result[0]);
        }

        [TestMethod]
        public void SplitTagListShouldReturnTrimmedValuesWhenItIsPassedMultiLine()
        {
            var result = BbsHelper.SplitTagList("TAG1\nTAG2 \r TAG3\r\n   TAG4   \r\n").ToList();
            Assert.AreEqual(4, result.Count());
            Assert.AreEqual("TAG1", result[0]);
            Assert.AreEqual("TAG2", result[1]);
            Assert.AreEqual("TAG3", result[2]);
            Assert.AreEqual("TAG4", result[3]);
        }

        [TestMethod]
        public void GetFromMessageNumberShouldReturnCorrectValue()
        {
            Assert.AreEqual(1, BbsHelper.GetFromMessageNumber(1, 20));
            Assert.AreEqual(21, BbsHelper.GetFromMessageNumber(2, 20));
        }

        [TestMethod]
        public void GetToMessageNumberShouldReturnCorrectValue()
        {
            Assert.AreEqual(1, BbsHelper.GetToMessageNumber(1, 20, 1));
            Assert.AreEqual(5, BbsHelper.GetToMessageNumber(1, 20, 5));
            Assert.AreEqual(20, BbsHelper.GetToMessageNumber(1, 20, 20));
            Assert.AreEqual(21, BbsHelper.GetToMessageNumber(2, 20, 1));
            Assert.AreEqual(40, BbsHelper.GetToMessageNumber(2, 20, 20));
        }

        [TestMethod]
        public void IsFirstPageShouldReturnCorrectValue()
        {
            Assert.IsTrue(BbsHelper.IsFirstPage(1));
            Assert.IsFalse(BbsHelper.IsFirstPage(2));
        }

        [TestMethod]
        public void IsLastPageShouldReturnCorrectValue()
        {
            Assert.IsTrue(BbsHelper.IsLastPage(1, 20, 1));
            Assert.IsTrue(BbsHelper.IsLastPage(1, 20, 5));
            Assert.IsTrue(BbsHelper.IsLastPage(1, 20, 20));
            Assert.IsFalse(BbsHelper.IsLastPage(1, 20, 21));
            Assert.IsTrue(BbsHelper.IsLastPage(2, 20, 21));
        }
    }
}
